﻿// 4. Write a program to convert hexadecimal numbers to their decimal representation.

using System;

class HexadecimalToDecimalNumbers
{
    static void Main()
    {
        Console.Write("Enter your hexadecimal number : ");
        string hexNumber = Console.ReadLine();

        int decimalNumber = ConvertIntoDecimal(hexNumber);

        Console.WriteLine("The decimal representation of the hexadecimal number {0} is {1}.", hexNumber, decimalNumber);
    }

    static int ConvertIntoDecimal(string hexNumber)
    {
        int decimalNumber = 0;
        int length = hexNumber.Length;
        int power = 0;

        for (int i = length - 1; i >= 0; i--)
        {
            int digit;

            switch (hexNumber[i])
            {
                case '0': digit = 0; break;
                case '1': digit = 1; break;
                case '2': digit = 2; break;
                case '3': digit = 3; break;
                case '4': digit = 4; break;
                case '5': digit = 5; break;
                case '6': digit = 6; break;
                case '7': digit = 7; break;
                case '8': digit = 8; break;
                case '9': digit = 9; break;
                case 'A': digit = 10; break;
                case 'B': digit = 11; break;
                case 'C': digit = 12; break;
                case 'D': digit = 13; break;
                case 'E': digit = 14; break;
                case 'F': digit = 15; break;
                default: digit = 16; break;
            }

            decimalNumber += (int)(digit * Math.Pow(16, power));
            power++;
        }

        return decimalNumber;
    }
}
